package com.test.drawing;

import java.util.Arrays;

import javax.microedition.khronos.opengles.GL10;

import com.test.loading.Frame;
import com.test.loading.Scene;
import com.test.positioning.Projector;

public class StaticImage extends Mesh {

	// Screen Information
	private float x;
	private float y;

	private float weight;
	private float height;

	private Projector screenProjector;

	public StaticImage(float screenX, float screenY, float weight,
			float height, Projector projector) {
		super(null);

		this.x = screenX;
		this.y = screenY;

		this.weight = weight;
		this.height = height;

		this.screenProjector = projector;

		setMeshBuffer(new MeshBuffer(createScene(x, y, this.weight,
				this.height)));

		setColor(1, 1, 1, 1);
	}

	public void findPosition() {
		
		float middleX = midP(x, weight);
		float middleY = midP(y, height);
		
		float[] position = getProjectorPointFromFarPlane(middleX, middleY);
		
		setX(position[0]);
		setY(position[1]);
		setZ(position[2]);
		
		System.out.println("Pos: " + Arrays.toString(getPosition()));
	}
	
	private float midP(float x, float y) {
		return (x + y)/2;
	}

	public Scene createScene(float x, float y, float weight, float height) {

		Scene scene = new Scene();

		// Vertices Creation

		float[] vertices = new float[4 * 3];
		int pointer = 0;
		
		// We need to get the Distance of the Square in FarPlane
		// Then we get...
		
		float[] verTopLeft = getProjectorPointFromFarPlane(x, y);
		float[] verBottomLeft = getProjectorPointFromFarPlane(x, y + height);
		float[] verBottomRight = getProjectorPointFromFarPlane(x + weight, y + height);
		float[] verTopRight = getProjectorPointFromFarPlane(x + weight, y);
		
		System.out.println("verTopL = " + Arrays.toString(verTopLeft));
		System.out.println("verBotL = " + Arrays.toString(verBottomLeft));
		System.out.println("verBotR = " + Arrays.toString(verBottomRight));
		System.out.println("verTopR = " + Arrays.toString(verTopRight));
		
		// Top Left Vertice
		vertices[pointer++] = verTopLeft[0]; // x
		vertices[pointer++] = verTopLeft[1]; // y
		vertices[pointer++] = 0; // z

		// Bottom Left Vertice

		vertices[pointer++] = verBottomLeft[0]; // x
		vertices[pointer++] = verBottomLeft[1]; // y
		vertices[pointer++] = 0; // z
		
		// Bottom Right Vertice

		vertices[pointer++] = verBottomRight[0]; // x
		vertices[pointer++] = verBottomRight[1]; // y
		vertices[pointer++] = 0; // z

		// Top Right Vertice

		vertices[pointer++] = verTopRight[0]; // x
		vertices[pointer++] = verTopRight[1]; // y
		vertices[pointer++] = 0; // z

		// Add Vertices
		scene.getFramesList().add(new Frame(vertices));

		scene.setIndices(new short[] { 0, 1, 2, 0, 2, 3 });

		// UV Texture Creation
		
		// UV Mapped to Image
		scene.setTexCoordinates(new float[] {
				0, 0, // Top Left
				0, 1, // Bottom Left
				1, 1, // Bottom Right
				1, 0}); // Top Right

		return scene;
	}

	public void loadVertices() {
		// Set this Scene in MeshBuffer
		getMeshBuffer().setScene(createScene(x, y, weight, height));
	}

	private float[] getProjectorPointFromFarPlane(float x, float y) {
		float[] farPlane = new float[3];
		screenProjector.unProject(new float[] { x, y, 1 }, 0, farPlane, 0);
		return farPlane;
	}

	@Override
	public void draw(GL10 gl) {
		
		loadVertices();
		findPosition();

		super.draw(gl);
		
//		enableFeatures(gl);

//		gl.glPushMatrix();
//
//		gl.glTranslatef(getX(), getY(), getZ());
//
//		drawElements(gl);
//
//		gl.glPopMatrix();

//		disableFeatures(gl);
	}
}
